﻿using B.S.BsicData.Api.Writer.Application.Command;
using B.S.BsicData.Domain;
using B.S.BsicData.ErrorCode.Entity;
using B.S.BsicData.Infartrans;
using MediatR;
using Microsoft.EntityFrameworkCore;
using System.Transactions;

namespace B.S.BsicData.Api.Writer.Application.CommandHandler
{
    public class DeleteWhouseCommandHandler : IRequestHandler<DeleteWhouseCommand, ApiResult<int>>
    {
        private readonly IBaseReository<WHouseModeles> whRepo;
        private readonly IBaseReository<LocationModeles> loRepo;
        private readonly IBaseReository<WHouseLocationModel> wlRepo;

        public DeleteWhouseCommandHandler(IBaseReository<WHouseModeles> whRepo, IBaseReository<LocationModeles> loRepo, IBaseReository<WHouseLocationModel> wlRepo)
        {
            this.whRepo = whRepo;
            this.loRepo = loRepo;
            this.wlRepo = wlRepo;
        }

        public async Task<ApiResult<int>> Handle(DeleteWhouseCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result = new ApiResult<int>();
            var model = whRepo.GetAll().Where(p => p.WhouseID == request.WhouseID).FirstOrDefault(); ;
            if (model != null)
            {
                var str = whRepo.Delete(model);
                if (str > 0)
                {
                    result.Code = ApiENums.成功;
                    result.Message = "成功";
                    result.Data = str;
                }
                else
                {
                    result.Code = ApiENums.失败;
                    result.Message = "失败";
                    result.Data = str;
                }
            }
            return await Task.FromResult(result);
        }
    }
}
